package com.mikufufu.blog.controller.admin;

import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.model.vo.admin.ImageVO;
import com.mikufufu.blog.storage.service.UploadService;
import com.mikufufu.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "用户上传文件模块")
@RequestMapping("/files")
public class FilesController {

    @Autowired
    private UploadService uploadService;

    /**
     * 上传文件 <br/>
     * <p> @RequestPart("file") 是为了解决上传文件的时候报错，也能让swagger正常使用上传文件的功能</p>
     * @param file 文件
     * @param pathName 文件路径
     * @return 文件路径
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件",dataType = "MultipartFile",paramType = "form",required = true),
            @ApiImplicitParam(name = "pathName",value = "文件路径",dataType = "String",paramType = "form")
    })
    public AjaxResult<String> upload(@RequestPart("file") MultipartFile file,String pathName){
        // 如果文件为空，则返回错误信息
        if (file.isEmpty()){
            return AjaxResult.error("文件不能为空");
        }
        // 如果文件路径为空，则使用默认路径
        if (StringUtils.isBlank(pathName)){
            return AjaxResult.data(uploadService.upload(file));
        }
        return AjaxResult.data(uploadService.upload(file,pathName));
    }

    /**
     * 上传图片 <br/>
     * {@code @RequestPart("file")} 是为了解决上传文件的时候报错，也能让swagger正常使用上传文件的功能
     * @param file 文件
     * @return 文件路径
     */
    @PostMapping("/uploadImage")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParam(name = "file",value = "文件",dataType = "MultipartFile",paramType = "form",required = true)
    public AjaxResult<ImageVO> uploadImage(@RequestPart("file") MultipartFile file, String pathName){
        // 如果文件为空，则返回错误信息
        if (file.isEmpty()){
            return AjaxResult.error("文件不能为空");
        }
        return AjaxResult.data(uploadService.uploadImage(file));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文件")
    @ApiImplicitParam(name = "fileName",value = "文件名",required = true)
    public AjaxResult<Boolean> delete(String fileName){
        return AjaxResult.status(uploadService.delete(fileName),"文件下载失败","文件下载失败");
    }

    @GetMapping("/download")
    @ApiOperation(value = "下载文件")
    @ApiImplicitParam(name = "fileName",value = "文件名",required = true)
    public AjaxResult<String> download(String fileName){
        return AjaxResult.data(uploadService.download(fileName));
    }

    @GetMapping("/downloadImage")
    @ApiOperation(value = "下载图片二进制流")
    @ApiImplicitParam(name = "fileName",value = "文件名",required = true)
    public void downloadImage(String fileName, HttpServletResponse response){
        try {
            if (StringUtils.isBlank(fileName)){
                throw new RuntimeException("文件名不能为空");
            }
            uploadService.downloadImage(fileName,response.getOutputStream());
        }catch (RuntimeException | IOException e){
            log.error("文件下载失败",e);
        }
    }

    @GetMapping("/listNotSubDir")
    @ApiOperation(value = "获取路径下的文件列表（不包含子文件夹）")
    @ApiImplicitParam(name = "path",value = "路径",required = true)
    public AjaxResult<List<Map<String,String>>> listNotSubDir(String path){
        return AjaxResult.data(uploadService.listObjects(path,false));
    }

    @GetMapping("/listAndSubDir")
    @ApiOperation(value = "获取路径下的文件列表（包含子文件夹）")
    @ApiImplicitParam(name = "path",value = "路径",required = true)
    public AjaxResult<List<Map<String,String>>> listAndSubDir(String path){
        return AjaxResult.data(uploadService.listObjects(path,true));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取指定前缀的对象列表")
    @ApiImplicitParam(name = "prefix",value = "前缀",required = true)
    public AjaxResult<List<?>> list(String prefix){
        return AjaxResult.data(uploadService.list(prefix));
    }
}
